package com.example.bes013.web.demo;

import com.bes.enterprise.springboot.embedded.BesServletWebServerFactory;
import com.bes.enterprise.webtier.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Configuration
public class BESPortConfig {
    @Value("${server.additionalPorts}")
    private String addtionalPorts;

    @Bean
    public BesServletWebServerFactory nextBES() {
        BesServletWebServerFactory bes = new BesServletWebServerFactory();
        bes.addAdditionalBesConnectors(additionalConnector());
        return bes;
    }

    private Connector[] additionalConnector() {
        if (Objects.isNull(this.addtionalPorts)) {
            return null;
        }
        String[] ports = this.addtionalPorts.split(",");
        List<Connector> result = new ArrayList<>();
        for (String port : ports) {
            Connector connector = new Connector("com.bes.enterprise.web.crane.http11.Http11NioProtocol");
            connector.setScheme("http");
            connector.setPort(Integer.valueOf(port));
            result.add(connector);
        }
        return result.toArray(new Connector[]{});
    }
}
